<template>
  <div>
    <!-- 添加或修改终端对话框 -->
    <el-dialog
      v-model="open"
      title="title"
      :close-on-click-modal="false"
    >
      <el-form
        ref="editForm"
        inline
        :model="form"
        label-width="80px"
      >
        <el-form-item label="费用类型">
          <el-input v-model="form.feeName" class="choice"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status">
            <el-option
              v-for="dict in dicts[DictDirection.通用状态]"
              :key="dict.dictValue"
              :value="dict.dictValue"
              :label="dict.dictLabel"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, watch, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import { getType, addType, updateType } from '@/api/system/dict'
import useDicts, { DictDirection } from '@/store/modules/dicts'
import { getCostNameList, addCostNameType, updateCostNameType, deleteCostNameType } from '@/api/BusinessConfig/costNameMain'

const dicts = useDicts()
dicts.setDicts(DictDirection.通用状态)
// 表单参数
let form = ref({
    feeName: undefined,
    status: undefined,
    id: undefined,
  }),
  // 表单校验
  rules = {
    feeName: [{ required: true, message: '必填', trigger: 'blur' }],
    status: [{ required: true, message: '必填', trigger: 'blur' }],
    dictCode: [{ required: true, message: '必填', trigger: 'blur' }],
  },
  // 标题
  title = ref(''),
  // 模态框显示
  open = ref(false),
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()

const emit = defineEmits(['changeList'])
// 重置新增修改列表
const reset = () => {
  form.value = {
    feeName: undefined,
    status: undefined,
    id: undefined,
  }
  $resetForm(editForm.value)
}
// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    console.log(form.value)
    if (form.value.id == undefined) {
      addCostNameType(form.value).then((res) => {
        if (res.code == 200) {
          ElMessage.success('新增成功')
          open.value = false
          reset()
          emit('changeList')
        }
      })
    } else {
      const dataList = JSON.parse(JSON.stringify(form.value))
      const { status } = dataList

      updateCostNameType(dataList).then((res) => {
        if (res.code == 200) {
          ElMessage.success('修改成功')
          form.value.status = status == '生效' ? 1 : status == '失效' ? 0 : ''
          console.log(form.value)
          reset()
          open.value = false
          emit('changeList')
        }
      })
    }
  })
}
// 修改新增——取消
const cancel = () => {
  open.value = false
  reset()
}

defineExpose({
  open,
  title,
  form,
})
</script>
